package com.example.app05.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.app05.entity.Task;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: cx
 * @date: 2022/5/2
 */
public class TaskDao {
    //增加数据
    public static Long insertData(SQLiteDatabase db, Task task){
        ContentValues cv = new ContentValues();
        cv.put("topic",task.getTopic());
        cv.put("doTime",task.getDoTime());
        cv.put("showTime",task.getShowTime());
        cv.put("newTime",task.getNewTime());
        cv.put("content",task.getContent());
        long dataId = db.insert("task_table",null,cv);
        return dataId;
    }
    //查询数据
    public static List<Task> queryData(SQLiteDatabase db) {
        List<Task> taskList = new ArrayList<>();
        Cursor cursor = db.query("task_table",null,null,null,null,null,null);
        while (cursor.moveToNext()){
            int indexId = cursor.getColumnIndex("id");
            int indexTopic = cursor.getColumnIndex("topic");
            int indexDoTime = cursor.getColumnIndex("doTime");
            int indexShowTime = cursor.getColumnIndex("showTime");
            int indexNewTime = cursor.getColumnIndex("newTime");
            int indexContent = cursor.getColumnIndex("content");
            taskList.add(new Task(cursor.getInt(indexId),cursor.getString(indexTopic),cursor.getString(indexDoTime)
                    ,cursor.getString(indexShowTime),cursor.getString(indexNewTime),cursor.getString(indexContent)));
        }
        return taskList;
    }
    //修改数据
    public static int updateData(SQLiteDatabase db,Task task,Integer id) {
        ContentValues cv = new ContentValues();
        cv.put("topic",task.getTopic());
        cv.put("doTime",task.getDoTime());
        cv.put("showTime",task.getShowTime());
        cv.put("newTime",task.getNewTime());
        cv.put("content",task.getContent());
        int n = db.update("task_table",cv,"id =?",new String[]{String.valueOf(id)});
        return n ;
    }
    //删除数据
    public static int deleteData(SQLiteDatabase db,Integer id) {
        String[] whereValue = new String[]{id.toString()};
        int n = db.delete("task_table","id=?",whereValue);
        return n;
    }
}
